//
//  LoginViewController.m
//  Eath
//
//  Created by yuhaya on 14-8-24.
//  Copyright (c) 2014年 yuhaya. All rights reserved.
//

#import "LoginViewController.h"
#import "Tool.h"
#import "User.h"
#import "findPwdViewController.h"
#import "CenterViewController.h"
#import "GenearchRegisterViewController.h"
#import "Database.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (IBAction)BgClick:(id)sender
{
    [self.Phone resignFirstResponder];
    [self.Password resignFirstResponder];
}

- (IBAction)BtnClick:(UIButton *)sender
{
    if(![sender.titleLabel.text isEqualToString:@"登    录"]){
        return;
    }
    
    if ([Tool networkStatus] == NO) {
        [Tool showMessage:@"无法连接网络！"];
        return;
    }    
    sender.titleLabel.text = @"登录中...";
    NSString * name = self.Phone.text;
    NSString * pwd = self.Password.text;
    
    void (^loginSuccess)() = ^(){
//        [Tool showMessage:@"登录成功！"];
        sender.titleLabel.text = @"登    录";
        [[Database sharedDatabase] syncData];
        [self.delegate loginOver];
    };
    NSUserDefaults * UserDefault = [NSUserDefaults standardUserDefaults];
    [UserDefault setObject:name forKey:@"user_name"];
    [UserDefault setObject:pwd forKey:@"user_pwd"];
    [UserDefault synchronize];
    
    void (^loginFail)(NSString *) = ^(NSString * msg){
        [Tool showMessage:msg];
        [self.delegate loginFail];
        sender.titleLabel.text = @"登    录";
    };
    [User loginByName:name andPwd:pwd andSuccessBlock:loginSuccess andFailBlock:loginFail];
    
}

- (IBAction)findPwdClick:(UIButton *)sender {
    findPwdViewController * findView = [[findPwdViewController alloc] init];
    [self.navigationController pushViewController:findView animated:YES];
}

- (IBAction)register:(UIButton *)sender {
    GenearchRegisterViewController * gen = [[GenearchRegisterViewController alloc] init];
    [self.navigationController pushViewController:gen animated:YES];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"登陆";
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.Phone.leftView = view;
    self.Phone.leftViewMode = UITextFieldViewModeAlways;
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.Password.leftView = view2;
    self.Password.leftViewMode = UITextFieldViewModeAlways;
    
    self.photo.layer.cornerRadius = self.photo.frame.size.width/2;
    self.photo.layer.shouldRasterize = YES;
    self.photo.layer.masksToBounds = YES;
    [self.photo.layer setBorderWidth:3];
    [self.photo.layer setBorderColor:[Tool colorWithHexString:@"#ffffff"].CGColor];
    
    CGRect frame = self.forget_password.frame;
    UIScreen * currentScreen = [UIScreen mainScreen];
    frame.origin.y = currentScreen.applicationFrame.size.height - 50;
    self.forget_password.frame = frame;

    
    
    [self.login.layer setMasksToBounds:YES];
    [self.login.layer setCornerRadius:4.0]; //设置矩圆角半径
// Do any additional setup after loading the view from its nib.
    
    NSUserDefaults * UserDefault = [NSUserDefaults standardUserDefaults];
    NSString * name = [UserDefault stringForKey:@"user_name"];
    NSString * pwd = [UserDefault stringForKey:@"user_pwd"];
    
    if(name != nil && pwd != nil){
        self.Phone.text = name;
        self.Password.text = pwd;
//        [self BtnClick:nil];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
